package com.pxxy.toturial.post;

import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

import java.util.Iterator;

/******************
 * @Author yejf
 * @Description Bean工厂后置处理器
 */
@Component   //此类也要被SPRING容器所管理
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    /*****
     * Spring框架会回调所有的BeanFactoryPostProcessor，并传入 ConfigurableListableBeanFactory 对象。
     * @param configurableListableBeanFactory 此参数的真正对象是：DefaultListableBeanFactory
     * @throws BeansException
     */
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory
                                                   configurableListableBeanFactory) throws BeansException {
        System.out.println("Spring框架回调了我们定义的 MyBeanFactoryPostProcessor");
        //我们在这里可以获取所有的Bean的定义
        /*final Iterator<String> iter = configurableListableBeanFactory.getBeanNamesIterator();
        //
        while(iter.hasNext()) {
            System.out.println("beanName = " + iter.next());
        }*/
        //获取每一个Bean的定义
        final String[] beanDefinitionNames = configurableListableBeanFactory.getBeanDefinitionNames();
        for(String bdn : beanDefinitionNames) {
            //
            final BeanDefinition beanDefinition = configurableListableBeanFactory.getBeanDefinition(bdn);
            System.out.println("beanName = " + bdn);
            final MutablePropertyValues propertyValues = beanDefinition.getPropertyValues();
            System.out.println("beanClass = "+beanDefinition.getBeanClassName());
            System.out.println("此Bean下的属性：\t"+propertyValues.toString());
        }
    }
}
